package leetcode.code2222;

import java.util.ArrayList;
import java.util.List;

import leetcode.IDebug;
import leetcode.helper.H;

public class Solution implements IDebug {

	public long numberOfWays(String s) {
		char[] cs = s.toCharArray();
		int len = cs.length;
		int[] zeros = new int[len];
		int[] ones = new int[len];
		int zero = 0;
		int one = 0;
		if (cs[0] == '1') {
			zeros[0] = 0;
			ones[0] = 1;
		} else {
			zeros[0] = 1;
			ones[0] = 0;
		}
		for (int i = 1; i < len; i++) {
			if (cs[i] == '1') {
				zeros[i] = zeros[i - 1];
				ones[i] = 1 + ones[i - 1];
			} else {
				zeros[i] = 1 + zeros[i - 1];
				ones[i] = ones[i - 1];
			}
		}
		if (cs[len - 1] == '1') {
			zero = 0;
			one = 1;
		} else {
			zero = 1;
			one = 0;
		}
		long ans = 0;
		for (int i = len - 2; i > 0; i--) {
			char c = cs[i];
			if (c == '1') {
				ans += zeros[i - 1] * zero;
				one += 1;
			} else {
				ans += ones[i - 1] * one;
				zero += 1;
			}
		}
		return ans;
	}

	@Override
	public void debug4() {
		H.compare(0, this.numberOfWays("11100"));

	}

	@Override
	public void debug3() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug2() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug1() {
		// TODO Auto-generated method stub

	}

	public static void main(String[] args) {
		Solution so = new Solution();
		so.debug1();
		so.debug2();
		so.debug3();
		so.debug4();
		List<String> list = new ArrayList<>();
		list.add("hello");
		System.out.println(list.contains("world"));
		System.out.println(list.contains("hello"));
		Object str = null;
		System.out.println(((String) str) == null);
	}

}
